<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="父节点" prop="parentId">
        <el-select
          v-model="form.parentId"
          :disabled="form.parentId === 0"
          placeholder="请选择父节点"
        >
          <el-option
            :label="form.parentName"
            style="height: auto; padding: 0"
            :value="form.parentId"
          >
            <el-tree
              ref="tree"
              :data="treeData"
              default-expand-all
              :props="defaultProps"
              @node-click="handleNodeClick"
            />
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="deptName">
        <el-input v-model="form.deptName" />
      </el-form-item>
      <el-form-item label="编码" prop="deptKey">
        <el-input v-model="form.deptKey" />
      </el-form-item>
      <el-form-item label="排序" prop="seqNum">
        <el-input v-model="form.seqNum" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button :loading="isSaving" type="primary" @click="save">
        确 定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doAdd, doEdit } from '@/api/dept'

  export default {
    name: 'DepartmentManagementEdit',
    props: {
      treeData: {
        type: Array,
        default: () => [],
        require: true,
      },
    },
    data() {
      return {
        // treeData: [],
        defaultProps: {
          children: 'childList',
          label: 'deptName',
        },
        form: {
          deptName: '',
          deptKey: '',
          seqNum: '',
          parentId: '',
          parentName: '',
        },
        rules: {
          parentId: [
            { required: true, trigger: 'blur', message: '请选择父节点' },
          ],
          deptName: [
            { required: true, trigger: 'blur', message: '请输入部门名称' },
          ],
          deptKey: [
            { required: true, trigger: 'blur', message: '请输入部门编码' },
          ],
          seqNum: [{ required: true, trigger: 'blur', message: '请输入序号' }],
        },
        title: '',
        dialogFormVisible: false,
        isSaving: false,
      }
    },
    created() {
      // this.fetchData()
    },
    methods: {
      handleNodeClick(node) {
        this.form.parentName = node.deptName
        this.form.parentId = node.id
      },
      showEdit(row) {
        console.log(this.treeData)
        if (!row) {
          this.title = '添加部门'
        } else {
          this.title = '编辑部门信息'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            try {
              let res
              let successMsg = '添加成功！'
              let errorMsg = '添加失败！'
              this.isSaving = true
              if (this.form.id !== undefined) {
                successMsg = '修改成功！'
                errorMsg = '修改失败！'
                res = await doEdit(this.form)
              } else {
                res = await doAdd(this.form)
              }
              if (res.status === 0) {
                this.$baseMessage(
                  successMsg,
                  'success',
                  'vab-hey-message-success'
                )
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(
                  res ? res.msg : errorMsg,
                  'error',
                  'vab-hey-message-error'
                )
              }
            } catch (e) {
              console.log(e)
              this.$baseMessage(
                e ? e.message : '操作失败',
                'error',
                'vab-hey-message-error'
              )
            }
            this.isSaving = false
          }
        })
      },
    },
  }
</script>
